package com.iotteam.androidbase.moudules.main.view;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.base.BaseActivity;
import com.iotteam.androidbase.commen.LocalInfoManager;
import com.iotteam.androidbase.commen.utils.CircularAnimUtil;
import com.iotteam.androidbase.commen.utils.DoubleClickExit;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.moudules.login.model.LoginInfo;
import com.iotteam.androidbase.moudules.login.view.LoginActivity;
import com.iotteam.androidbase.moudules.login.view.UserInfoActivity;
import com.iotteam.androidbase.moudules.main.MainPagerAdapter;
import com.iotteam.androidbase.moudules.sensor_add.view.AddSensorActivity;
import com.iotteam.androidbase.moudules.test_sensor.TestSensorActivity;

import java.nio.charset.MalformedInputException;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private FloatingActionButton mFab;
    private DrawerLayout mDrawer;
    private ActionBarDrawerToggle mToggle;
    private NavigationView mNavigationView;
    private MainPagerAdapter mHomePagerAdapter;
    private ViewPager mViewPager;
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private DrawerLayout mMainDrawer;
    private TextView mAccountText;
    private MenuItem mLoginMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);

        mFab = (FloatingActionButton) findViewById(R.id.fab);
        mFab.setImageResource(android.R.drawable.ic_dialog_email);
        mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(MainActivity.this, R.color.colorAccent)));
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mToggle = new ActionBarDrawerToggle(
                this, mDrawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawer.setDrawerListener(mToggle);
        mToggle.syncState();

        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        mNavigationView.setNavigationItemSelectedListener(this);

        mAccountText = (TextView) mNavigationView.getHeaderView(0).findViewById(R.id.tv_account);
        Menu mMenu = mNavigationView.getMenu();
        mLoginMenuItem = mMenu.findItem(0).getSubMenu().getItem(1);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                mFab.post(new Runnable() {
                    @Override
                    public void run() {

                        mFab.hide(new FloatingActionButton.OnVisibilityChangedListener() {
                            @Override
                            public void onHidden(FloatingActionButton fab) {
                                if(position == 1){
                                    mFab.setImageResource(R.drawable.ic_add_24dp);
                                    mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)));
                                    mFab.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            Intent intent = new Intent(MainActivity.this, AddSensorActivity.class);
                                            //intent.putExtra(C.MULTI_CHECK, true);
                                            CircularAnimUtil.startActivity(MainActivity.this, intent, mFab,
                                                    R.color.colorPrimary);
                                        }
                                    });

                                }else if(position == 0){
                                    mFab.setImageResource(android.R.drawable.ic_dialog_email);
                                    mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(MainActivity.this, R.color.colorAccent)));
                                    mFab.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {

                                        }
                                    });
                                }
                                fab.show();
                            }
                        });
                    }
                });
                if (!mFab.isShown()) {
                    mFab.show();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        LoginInfo info = LocalInfoManager.getInstance().getUserInfo();
        if(info != null){
            mAccountText.setText(info.getMobile());
            mLoginMenuItem.setTitle(R.string.label_activity_userinfo);
        }else{
            mAccountText.setText("");
            mLoginMenuItem.setTitle(R.string.label_activity_login);
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            //super.onBackPressed();
            if (!DoubleClickExit.check()) {
                ToastUtil.showShort(getString(R.string.double_exit));
            } else {
                finish();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_device_info) {
            mViewPager.setCurrentItem(1);
        } else if (id == R.id.nav_device_location) {

        } else if (id == R.id.nav_device_setting) {

        } else if (id == R.id.nav_app_setting){

        } else if (id == R.id.nav_login) {
            if(!LocalInfoManager.getInstance().isLogin()){
                LoginActivity.launch(MainActivity.this);
            }else{
                UserInfoActivity.launch(MainActivity.this);
            }
        } else if (id == R.id.nav_about) {

        } else if (id == R.id.nav_test) {
            TestSensorActivity.launch(MainActivity.this);
        }

        mMainDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mMainDrawer.closeDrawer(GravityCompat.START);

        return true;
    }

    private void initView(){
        mHomePagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
        mHomePagerAdapter.addTab(new CommenInfoFragment(), "综合信息");
        mHomePagerAdapter.addTab(new SensorListFragment(), "硬件列表");
        mViewPager.setAdapter(mHomePagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager, false);
    }
}
